<template>
  <div class="app-container">
    <el-row style="margin-bottom: 0">
      <!-- 查询表单 -->
      <el-col :span="20">
        <el-form :inline="true" size="medium">
          <el-form-item label="课程">
            <el-input v-model="searchObj.title" clearable placeholder="标题" style="width:200px"/>
          </el-form-item>
          <el-form-item>
            <el-select v-model="searchObj.teacherId" clearable placeholder="讲师" style="width:150px">
              <el-option
                v-for="teacher in teacherList"
                :key="teacher.id"
                :label="teacher.name"
                :value="teacher.id"
              />
            </el-select>
          </el-form-item>
          <!-- 所属科目 级联下拉列表 -->
          <!-- 一级科目 -->
          <el-form-item label="所属科目">
            <el-select
              v-model="searchObj.subjectParentId"
              placeholder="一级科目"
              clearable
              @change="primarySubjectChanged"
              style="width:150px"
            >
              <el-option
                v-for="subject in primarySubjectList"
                :key="subject.id"
                :label="subject.title"
                :value="subject.id"
              />
            </el-select>
            <!-- 二级科目 -->
            <el-select v-model="searchObj.subjectId" placeholder="二级科目" clearable style="width:150px;margin-left: 10px">
              <el-option
                v-for="subject in secondarySubjectList"
                :key="subject.id"
                :label="subject.title"
                :value="subject.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button-group>
              <el-button type="primary" icon="el-icon-search" @click="fetchData()">查询</el-button>
              <el-button type="warning" icon="el-icon-refresh-right" @click="resetData()">重置</el-button>
            </el-button-group>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
    <!-- 表格 -->
    <el-row>
      <el-col :span="24">
        <el-table
          :data="list"
          :header-cell-style="{'text-align':'center'}"
          fit
          stripe
          tooltip-effect="light"
          v-loading="loading"
          highlight-current-row
          element-lodaing-text="数据加载中"
          @selection-change="handleSelectionChange"
        >
          <!--          <el-table-column type="selection" min-width="2%" align="center"/>-->
          <el-table-column type="index" label="#" min-width="2%" align="center">
            <template slot-scope="scope">
              {{ (page - 1) * limit + scope.$index + 1 }}
            </template>
          </el-table-column>
          <el-table-column :filters="filters" :filter-method="filterHandler"
                           prop="status" label="状态" min-width="7%" align="center"
          >
            <template slot-scope="scope">
              <el-tag v-if="scope.row.status === 'draft'" effect="dark" type="warning" size="medium">
                未发布
              </el-tag>
              <el-tag v-if="scope.row.status === 'pass'" effect="dark" type="success" size="medium">
                已发布
              </el-tag>
              <el-tag v-if="scope.row.status === 'fail'" effect="dark" type="info" size="medium">
                已下架
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column label="标题 / 科目" min-width="20%">
            <template slot-scope="scope">
              <el-row>
                <span class="info-label">标题</span>
                {{ scope.row.title }}
                <span v-if="scope.row.status === 'pass'">
                  <a :href="USER_API + '/course/' + scope.row.id" target="_blank">
                    <i class="el-icon-link"/>
                  </a>
                </span>
              </el-row>
              <el-row style="margin-top:15px">
                <span class="info-label">科目</span>
                {{ scope.row.subjectParentTitle }}
                <i class="el-icon-arrow-right"></i>
                {{ scope.row.subjectTitle }}
              </el-row>
            </template>
          </el-table-column>
          <el-table-column label="课时 / 人次" min-width="10%">
            <template slot-scope="scope">
              <p><span class="info-label" v-html="'课&emsp;&emsp;时'"/>{{ scope.row.lessonNum }}</p>
              <p><span class="info-label">浏览次数</span>{{ scope.row.viewCount }}</p>
              <p><span class="info-label">购买人数</span>{{ scope.row.buyCount }}</p>
            </template>
          </el-table-column>
          <el-table-column prop="teacherName" label="讲师" min-width="8%" align="center">
            <template slot-scope="scope">
              <el-row style="margin-bottom:5px">
                <el-avatar :size="50" :src="scope.row.teacherAvatar" fit="cover"/>
              </el-row>
              <el-row>
                {{ scope.row.teacherName }}
                <a :href="USER_API + '/teacher/' + scope.row.teacherId" target="_blank">
                  <i class="el-icon-link"/>
                </a>
              </el-row>
            </template>
          </el-table-column>
          <el-table-column prop="price" label="价格 / 元" min-width="7%" align="center">
            <template slot-scope="scope">
              <span v-if="Number(scope.row.price) === 0" class="price free">限时免费</span>
              <span v-else class="price">{{ scope.row.price }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="cover" label="封面" min-width="18%" align="center">
            <template slot-scope="scope">
              <el-image :src="scope.row.cover" fit="cover" class="cover-img"/>
            </template>
          </el-table-column>
          <el-table-column prop="gmtCreate" label="上架时间" sortable min-width="10%" align="center">
            <template slot-scope="scope">
              <i class="el-icon-date"></i>
              {{ scope.row.gmtCreate }}
            </template>
          </el-table-column>
          <el-table-column label="操作" min-width="16%" align="center">
            <template slot-scope="scope">
              <el-row style="margin-bottom:5px">
                <el-button type="success" icon="el-icon-edit" size="small" plain @click="editInfo(scope.row.id)">
                  信息
                </el-button>
                <el-button type="primary" icon="el-icon-edit" size="small" plain @click="editChapter(scope.row.id)">
                  章节
                </el-button>
              </el-row>
              <el-row>
                <el-button type="info" icon="el-icon-circle-close" size="small" plain @click="fail(scope.row.id)"
                           :disabled="scope.row.status==='fail'"
                >
                  下架
                </el-button>
                <el-button type="danger" icon="el-icon-delete" size="small" plain @click="removeById(scope.row.id)">
                  删除
                </el-button>
              </el-row>
            </template>
          </el-table-column>
        </el-table>
      </el-col>
    </el-row>
    <!-- 分页组件 -->
    <el-row>
      <el-col :span="24">
        <el-pagination
          :current-page="page"
          :total="total"
          :page-size="limit"
          :page-sizes="[5, 10, 15, 20, 50, 100]"
          style="padding: 30px 0; text-align: center;"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="changePageSize"
          @current-change="changeCurrentPage"
        />
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { courseApi, subjectApi, teacherApi } from '@/api'

export default {
  data() {
    return {
      loading: true, // 加载遮罩
      list: [], // 课程列表
      total: 0, // 总记录数
      page: 1, // 页码
      limit: 5, // 每页记录数
      searchObj: {
        subjectId: ''// 解决查询表单无法选中二级科目
      }, // 查询条件
      filters: [
        { text: '未发布', value: 'draft' },
        { text: '已发布', value: 'pass' },
        { text: '已下架', value: 'fail' }
      ],// 审核状态过滤器
      teacherList: [], // 讲师列表
      primarySubjectList: [], // 一级科目列表
      secondarySubjectList: [], // 二级科目列表
      selection: [], // 选中的行
      USER_API: process.env.VUE_APP_WEB_API
    }
  },
  created() {
    this.fetchData()
    this.fetchSubjectList()
    this.fetchTeacherList()
  },
  methods: {
    // 获取课程列表
    fetchData() {
      // 开始加载
      this.loading = true
      // 调用api
      courseApi.queryByPage(this.page, this.limit, this.searchObj).then(response => {
        this.list = response.data.items
        this.total = response.data.total
        // 结束加载
        this.loading = false
      })
    },
    // 获取科目列表
    fetchTeacherList() {
      teacherApi.queryAll().then(response => {
        this.teacherList = response.data.items
      })
    },
    // 获取科目列表
    fetchSubjectList() {
      subjectApi.getTreeList().then(response => {
        this.primarySubjectList = response.data.items
      })
    },
    removeById(id) {
      this.$confirm('将会删除这条记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        return courseApi.deleteById(id)
      }).then(() => {
        this.fetchData()
        this.$message.success('删除成功')
      }).catch(error => {
        if (error === 'cancel') {
          this.$message.info('取消删除')
        }
      })
    },
    // 编辑信息
    editInfo(id) {
      this.$router.push('/course/info/' + id)
    },
    editChapter(id) {
      this.$router.push('/course/chapter/' + id)
    },
    // 下架
    fail(id) {
      courseApi.updateStatus(id, 'fail').then(response => {
        this.fetchData()
        this.$message.success(response.message)
      })
    },
    // 一级科目改变时
    primarySubjectChanged(value) {
      for (let i = 0; i < this.primarySubjectList.length; i++) {
        if (this.primarySubjectList[i].id === value) {
          this.secondarySubjectList = this.primarySubjectList[i].children
          this.searchObj.subjectId = ''
        }
      }
    },
    // 每页记录数改变时
    changePageSize(size) {
      this.limit = size
      this.fetchData()
    },
    // 页码改变时
    changeCurrentPage(page) {
      this.page = page
      this.fetchData()
    },
    // 重置表单
    resetData() {
      this.searchObj = {}
      this.secondarySubjectList = [] // 二级科目列表
      this.fetchData()
    },
    // 复选框选项发生变化时
    handleSelectionChange(selection) {
      this.selection = selection
    },
    // 过滤器方法
    filterHandler(value, row) {
      console.log(value)
      return row.status === value
    }
  }
}
</script>

<style lang="scss">
@import "~@/assets/css/table_head_align.scss";

.cover-img {
  width: 213px;
  height: 119px;
  border-radius: 10px;
  margin: 0 auto -6px;
}

.info-label {
  margin-right: 10px;
  color: #909399;
}

.price {
  color: #FFC233;
  font-weight: bold;
  font-family: "Microsoft YaHei";
}

.free {
  color: #42B983;
}

.el-button + .el-button {
  margin-left: 5px;
}
</style>
